/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import BO.DeviceBO;
import BO.ReportBO;
import BO.UserBO;
import java.util.Date;
import java.util.Map;
import javax.faces.component.UIParameter;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import pojo.Report;
import pojo.User;
import ulti.DateUlti;

/**
 *
 * @author phong
 */
public class ReportBean {

    private String deviceId;
    private String title;
    private String content;
    private Report report;

    public ReportBean() {
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Report getReport() {
        return report;
    }

    public void setReport(Report report) {
        this.report = report;
    }

    //<editor-fold defaultstate="collapsed" desc="Lấy thông tin Report thông qua id">
    public Report getReportByID(int id) {
        report = ReportBO.getReportByID(id);
        return report;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="User gui 1 Report">
    public String doReport() {
        Report report = new Report();
        report.setDevice(DeviceBO.getDeviceByID(deviceId));
        report.setTitle(title);
        report.setContent(content);
        report.setCreateDate(new Date());
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Map session = facesContext.getExternalContext().getSessionMap();
        User user = (User) session.get("User");
        report.setUserBySender(UserBO.getUserByID(user.getId()));
        return ReportBO.doReport(report);
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Technican confirm 1 Report">
    public void confirmReport(ActionEvent e) {
        UIParameter reportId = (UIParameter) e.getComponent().findComponent("reportID");
        Report report = ReportBO.getReportByID((Integer) (reportId.getValue()));
        report.setStatus("Confirm");
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Map session = facesContext.getExternalContext().getSessionMap();
        User user = (User) session.get("User");
        report.setUserByTechnician(UserBO.getUserByID(user.getId()));
        ReportBO.changeStatusReport(report);
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Technican ket thuc 1 Report">
    public void denyReport(ActionEvent e) {
        UIParameter reportId = (UIParameter) e.getComponent().findComponent("reportDenyID");
        Report report = ReportBO.getReportByID((Integer) (reportId.getValue()));
        report.setStatus("In Process");
        report.setUserByTechnician(null);
        ReportBO.changeStatusReport(report);
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Technican ket thuc 1 Report">
    public void finishReport(ActionEvent e) {
        UIParameter reportId = (UIParameter) e.getComponent().findComponent("reportDoneID");
        Report report = ReportBO.getReportByID((Integer) (reportId.getValue()));
        report.setStatus("Done");
        report.setEndDate(DateUlti.getNow());
        ReportBO.changeStatusReport(report);
    }
    //</editor-fold>    

    //<editor-fold defaultstate="collapsed" desc="Kiem tra 1 Report da hoan thanh chua">
    public boolean checkDoneReport(int reportID) {
        return ReportBO.checkDoneReport(reportID);
    }
    //</editor-fold>
}
